/* eslint-disable */
import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../views/Login.vue'
import TeacherLayout from '../views/teacher/Layout.vue'
import TeacherClasses from '../views/teacher/Classes.vue'
import TeacherCourseDetail from '../views/teacher/CourseDetail.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    name: 'Login',
    component: Login
  },
  {
    path: '/admin',
    component: () => import('../views/admin/Layout.vue'),
    redirect: '/admin/dashboard',
    children: [
      {
        path: 'dashboard',
        name: 'AdminDashboard',
        component: () => import('../views/admin/Dashboard.vue')
      },
      {
        path: 'teachers',
        name: 'AdminTeachers',
        component: () => import('../views/admin/Teachers.vue')
      },
      {
        path: 'profile',
        name: 'AdminProfile',
        component: () => import('../views/admin/Profile.vue')
      },
      {
        path: 'announcements',
        name: 'AdminAnnouncements',
        component: () => import('../views/admin/Announcements.vue')
      },
      {
        path: 'departments',
        name: 'AdminDepartments',
        component: () => import('../views/admin/Departments.vue'),
        meta: {
          title: '院系管理'
        }
      },
      {
        path: 'classes',
        name: 'AdminClasses',
        component: () => import('../views/admin/Classes.vue'),
        meta: { title: '班级管理' }
      },
      {
        path: 'courses',
        name: 'AdminCourses',
        component: () => import('../views/admin/Courses.vue'),
        meta: { title: '课程管理' }
      }
    ]
  },
  {
    path: '/teacher',
    component: TeacherLayout,
    redirect: '/teacher/classes',
    children: [
      {
        path: 'classes',
        name: 'TeacherClasses',
        component: TeacherClasses
      },
      {
        path: 'course/:id',
        name: 'TeacherCourseDetail',
        component: TeacherCourseDetail
      },
      {
        path: 'profile',
        name: 'TeacherProfile',
        component: () => import('../views/teacher/Profile.vue')
      },
      {
        path: 'quiz/:quizId',
        name: 'TeacherQuizDetail',
        component: () => import('../views/teacher/QuizDetail.vue')
      }
    ]
  },
  {
    path: '/student',
    component: () => import('../views/student/Layout.vue'),
    redirect: '/student/dashboard',
    children: [
      {
        path: 'dashboard',
        name: 'StudentDashboard',
        component: () => import('../views/student/Dashboard.vue')
      },
      {
        path: 'course/:id',
        name: 'StudentCourseDetail',
        component: () => import('../views/student/CourseDetail.vue'),
        props: (route) => ({
          id: route.params.id,
          classId: route.query.classId
        })
      },
      {
        path: 'profile',
        name: 'StudentProfile',
        component: () => import('../views/student/Profile.vue')
      },
      {
        path: 'assignments',
        name: 'StudentAssignments',
        component: () => import('../views/student/Assignments.vue')
      },
      {
        path: 'announcements',
        name: 'StudentAnnouncements',
        component: () => import('../views/student/Announcements.vue')
      }
    ]
  }
]

const router = new VueRouter({
  routes
})

// 导航守卫
router.beforeEach((to, from, next) => {
  const token = localStorage.getItem('token')
  if (to.path === '/login') {
    next()
  } else {
    if (!token) {
      next('/login')
    } else {
      next()
    }
  }
})

export default router 